package com.turkcell.dv.rest;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.eolcum.evocenter.Action;
import com.eolcum.evocenter.ActionBuffer;
import com.google.gson.Gson;
import com.turkcell.dv.service.RequestScopedContext;

@RestController
@RequestMapping(value = "/apartmentService", headers = "Content-Type=application/json", produces = "application/json")
public class ApartmentService {
	
	@Autowired private ActionBuffer buffer;
	@Autowired private RequestScopedContext rsc;
	
	@RequestMapping(value= "/setApartmentInfo", method = RequestMethod.POST)
	@ResponseBody
	public String setApartmentInfo(@RequestBody String request) {
		Gson gson = new Gson();
		Action requestDTO = gson.fromJson(request, Action.class);
		
		
		List<Action> actionList = buffer.getActionList(rsc.getUserId().toString());
		for(int i = 0; i < actionList.size(); i++) {
			if(actionList.get(i).getAction() == Action.ACTION_GET_APARTMENT_INFO) {
				actionList.set(i, requestDTO);
			}
		}
		return "OK";
	}

	@RequestMapping(value= "/getApartmentInfo", method = RequestMethod.GET)
	@ResponseBody
	public String getApartmentInfo() {
		String id = rsc.getUserId().toString();
		List<Action> actionList = buffer.getActionList(id);
		for(int i = 0; i < actionList.size(); i++) {
			if(actionList.get(i).getAction() == Action.ACTION_GET_APARTMENT_INFO) {
				actionList.get(i).setTimeStamp(Calendar.getInstance().getTimeInMillis());
				return actionList.get(i).getResponseJSON();
			}
		}
		return null;
	}
}
